package hibernate;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import hibernate.Bean;


/**
 * Created with IntelliJ IDEA.
 * User: shprotik
 * Date: 05.04.12
 * Time: 0:33
 * To change this template use File | Settings | File Templates.
 */
public class HibernateOperations {
    public void insert(String fname, String sname, String lname) {

        Bean inf = new Bean();
        inf.setFirstName(fname);
        inf.setSecondName(sname);
        inf.setLastName(lname);
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        session.save(inf);
        tx.commit();
        session.close();


    }

    public void delete(String fname, String sname, String lname) {

        Session session  = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        session.createQuery("DELETE FROM Bean WHERE firstName ='" + fname + "' AND secondName ='" + sname + "' AND lastName = '" + lname + "'").executeUpdate();
        tx.commit();
        session.close();
    }
    public List<Bean> getAll()
    {

        Session session  = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        Query q1 = session.createQuery("select *FROM Bean");
        List<Bean> fio = q1.list();
        return fio;
    }

}
